package org.geo.common.util;

import java.security.MessageDigest;

/**
 * 字符串工具
 *
 * @author ljy
 */
public class StringUtil {
    /**
     * MD5加密
     *
     * @param plainText 明文
     * @return 密文
     */
    public static String md5Encrypt(String plainText) {
        MessageDigest messageDigest;
        String md5Sum = "";
        try {
            messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(plainText.getBytes("utf-8"));
            byte[] bytes = messageDigest.digest();
            int byteToInt;
            StringBuilder buf = new StringBuilder();
            for (byte bt : bytes) {
                byteToInt = bt;
                if (byteToInt < 0) {
                    byteToInt += 256;
                }
                if (byteToInt < 16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(byteToInt));
            }
            md5Sum = buf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return md5Sum;
    }

}
